package com.tm.fiverole.security.service;

import com.tm.fiverole.entity.MenuEntity;
import com.tm.fiverole.mapper.MenuMapper;
import com.tm.fiverole.security.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 新建一个用于判断是否有访问权限的类
 */
@Component
public class PermissionEvaluatorImpl implements PermissionEvaluator {

    @Autowired
    private MenuMapper mapper;

    @Override
    public boolean hasPermission(Authentication authentication, Object targetUrl, Object permission) {
        // 获取用户信息
        User user =(User) authentication.getPrincipal();
        // 查询用户权限(这里可以将权限放入缓存中提升效率)

        List<String> sysMenuEntityList = mapper.querySysMenuListByUserId(user.getId());

        // 权限对比
        return sysMenuEntityList.contains(permission.toString());
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable serializable, String s, Object o) {
        return false;
    }
}